package com.todaynews.ui.home;

import android.text.TextUtils;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.todaynews.R;
import com.todaynews.frame.cache.SpCache;
import com.todaynews.frame.model.InfoResult;
import com.todaynews.frame.okhttp.OkEntityListRequest;
import com.todaynews.frame.okhttp.OkHttpRequest;
import com.todaynews.frame.ui.fragment.BaseFragment;
import com.todaynews.frame.ui.views.brvah.BaseQuickAdapter;
import com.todaynews.ui.home.adapter.NewsListAdapter;
import com.todaynews.ui.home.adapter.VideoListAdapter;
import com.todaynews.model.model.News;
import com.todaynews.model.model.NewsData;
import com.todaynews.ui.home.views.TipView;
import com.todaynews.utils.Constants;
import com.todaynews.utils.GsonUtils;
import com.todaynews.utils.ListUtils;
import com.todaynews.utils.UIUtils;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

public class NewsListFragment extends BaseFragment {

    private static String channel_code;//新闻分类ID;
    @BindView(R.id.tip_view)
    TipView tipView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private boolean isVideoList;
    private boolean isRecommendChannel;//是否是推荐频道
    private List<News> newsList = new ArrayList<>();
    protected BaseQuickAdapter newsAdapter;
    private LinearLayoutManager linearLayoutManager;

    public static NewsListFragment newInstance(String argument) {
        channel_code = argument;
        return new NewsListFragment();
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_news_list;
    }

    @Override
    public void init() {
        channel_code = getArguments().getString(Constants.CHANNEL_CODE);
        String[] channelCodes = UIUtils.getInstance().getStringArr(R.array.channel_code);
        isRecommendChannel = channel_code.equals(channelCodes[0]);//是否是推荐频道
        isVideoList = channel_code.endsWith(channelCodes[1]);

        if (isVideoList) {
            //如果是视频列表
            newsAdapter = new VideoListAdapter(newsList);
        } else {
            //其他新闻列表
            newsAdapter = new NewsListAdapter(channel_code, newsList);
        }
        linearLayoutManager = new LinearLayoutManager(activity);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(newsAdapter);
    }

    @Override
    public void bind(View view) {
        super.bind(view);
    }

    @Override
    public void loadData() {
        getNews();
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

        switch (what){
            case R.id.getNews:
                String desc = t.getTips().getDisplay_info();
                List<NewsData> data = (List<NewsData>) t.getT();
                List<News> list = new ArrayList<>();
                if (data != null && data.size()>0){
                    for(int i=0;i<data.size();i++){
                        News news = GsonUtils.getInstance().jsonToClass(data.get(i).getContent(), News.class);
                        list.add(news);
                    }
                }
                if (list != null && list.size()>0) {
                    if (TextUtils.isEmpty(list.get(0).getTitle())) {
                        //由于汽车、体育等频道第一条属于导航的内容，所以如果第一条没有标题，则移除
                        list.remove(0);
                    }
                }
                dealRepeat(list);//处理新闻重复问题
                newsList.addAll(0, list);
                newsAdapter.notifyDataSetChanged();
                tipView.show(desc);
                break;
                default:break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    /**
     * 处理置顶新闻和广告重复
     */
    private void dealRepeat(List<News> info) {
        if (isRecommendChannel && !ListUtils.getInstance().isEmpty(newsList)) {
            //如果是推荐频道并且数据列表已经有数据,处理置顶新闻或广告重复的问题
            newsList.remove(0);//由于第一条新闻是重复的，移除原有的第一条
            //新闻列表通常第4个是广告,除了第一次有广告，再次获取的都移除广告
            if (info.size() >= 4) {
                News fourthNews = info.get(3);
                //如果列表第4个和原有列表第4个新闻都是广告，并且id一致，移除
                if (fourthNews.tag.equals(Constants.ARTICLE_GENRE_AD)) {
                    info.remove(fourthNews);
                }
            }
        }
    }

    private void getNews() {
        long lastTime;
        lastTime = SpCache.getInstance().get(channel_code, 0);//读取对应频道下最后一次刷新的时间戳
        if (lastTime == 0) {
            //如果为空，则是从来没有刷新过，使用当前时间戳
            lastTime = System.currentTimeMillis() / 1000;
        }
        OkEntityListRequest<NewsData> request = new OkEntityListRequest<>(R.id.getNews, Constants.NEWS,
                NewsData.class);
        request.setEnumRequestWay(OkHttpRequest.EnumRequestWay.GET);
        request.addParams("category", channel_code);
        request.addParams("min_behot_time", String.valueOf(lastTime));
        request.addParams("last_refresh_sub_entrance_interval", String.valueOf(System.currentTimeMillis() / 1000));
        requestOkhttpEntityList(request);
    }

}
